<?php
/* 定义属性 */
define ("CONfilename", "./config/set.php");
define ("DDUDB","./class/db.class.php");
define ("LOADINDEX","./class/load.class.php");
define ("PLUIN", "/plu-in/");//以后调用plu-in里的插件
/* 為系統作配置準備，方便以後系統引用數據庫 */
session_start();
require_once(CONfilename);/*讀設置*/
require_once(DDUDB);/* 載入數據庫模塊 */
require_once(LOADINDEX);/* 載入引導模塊 */

/* 配置屬性 */
$returnajaxcontent ="";
/*下面建立數據連接，添加數據庫模塊到變量，使db變量可以直接控制sql。  */
$db = new CDB();
$MEMBER = new member();
$ROOT = new root();
/* 收到信息 */
if (empty($_GET['action'])) {
	$action = '';
}else {
	$action = $_GET['action'];
	/* 操作判斷 */
	switch ($action){
		case 'viewgd':
		$returnajaxcontent = viewgd($_GET['cn'],$_GET['time']);
		break;
		case 'progress':
		$returnajaxcontent = showinprogress($_GET['cn'],$_GET['time']);
		break;
		case 'sumbitprogress':
		$returnajaxcontent = sumbitgdpg($_GET['cn'],$_GET['time'],$_POST);
		break;
		case 'submitzthhgd':
		$returnajaxcontent = submitzthhgd($_GET['cn'],$_GET['time'],$_POST);
		break;
		case 'zthh':
		$returnajaxcontent = showzthh($_GET['cn'],$_GET['time']);
		break;
		case 'updatehj':
		$returnajaxcontent = updatehj($_GET['cn'],$_GET['begintime']);
		break;
		case 'updatezthhgd':
		$returnajaxcontent = submitzthhud($_GET['cn'],$_GET['time'],$_POST);
		break;
	}
}
function viewgd($cn,$time){
	global $db;
	$xml = file_get_contents("./tpl/tpl.viewgd.html");
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$table_change=array(
		'{content}' => $db->Get('content'),
		'{orderman}' => $db->Get('orderman'),
		'{isend}' => $db->Get('isend'),
		'{archivecontent}' => $db->Get('archivecontent'),
		'{ctrlman}' => $db->Get('ctrlman'),
		'{customername}' => $db->Get('customername'),
		'{submitnumber}' => $db->Get('submitnumber'),
		'{contactnumber}' => $db->Get('contactnumber'),
		'{idnumber}' => $db->Get('idnumber'),
		'{idcardaddress}' => $db->Get('idcardaddress'),
		'{qctr}' =>getlinkqc($cn),
		'{cn}' => $cn,
		'{time}' =>$time,
		'{hjtr}' =>serachhj($cn),
		);
		$xml=strtr($xml,$table_change);
	}
	return $xml;
}
function serachhj($cn){
	global $db;
	$df = '<tr><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无</td><td>无权</td></tr>';
	$xml = '';
	$db->ExeQuerySql("SELECT * FROM `cu_pg` WHERE  gd_cn = '$cn'");
	for ($i = 0; $i < $db->db->row_count; $i++) {
		$row = $db->GetFetchArray();	
		$xml=$xml.'<tr><td>'.$row['pg_name'].'</td><td>'.$row['pg_begintime'].'</td><td>'.$row['pg_rechtime'].'</td><td>'.$row['pg_yjbx'].'</td><td>'.$row['pg_sjbx'].'</td><td>'.$row['traceman'].'</td><td>'.$row['oman'].'</td><td>'.$row['bz'].'</td><td>'.$row['bh'].'</td><td><a href="javascript:void(0)" onclick="updatehj(\''.$cn.'\',\''.$row['pg_begintime'].'\')">更正</a></td></tr>';
	 }
	 if($xml==''){
		 $xml = $df;
	 }
	 return $xml;
}
function getlinkqc($cn){
	global $db;
	$xml = '';
	$db->ExeQuerySql("SELECT * FROM `cu_qcxq` WHERE  checknumber='$cn'");
	for ($i = 0; $i < $db->db->row_count; $i++) {
		$row = $db->GetFetchArray();	
		$xml=$xml.'<tr><td>'.$row['hwyname'].'</td><td>'.$row['clname'].'</td><td>'.$row['headman'].'</td><td>'.$cn.'</td><td>'.$row['sn'].'</td><td><a href="'.$row['link'].'" target="new">链接</a></td><td>'.$row['baseappraise'].'</td></tr>';
	 }
	 if($xml==''){
		 $xml = '<tr><td>无</td><td>无</td><td>无</td><td>'.$cn.'</td><td>无</td><td>无</td><td>无</td></tr>';
	 }
	 return $xml;
}
function showinprogress($cn,$time){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if($gdclroot==true){
		$xml = file_get_contents("./tpl/tpl.showinprogress.html");
		$table_change=array(
			'{cn}' => $cn,
			'{time}' => $time,
			'{now_em}' => $now_em
		);
		$xml=strtr($xml,$table_change);
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div><img src="../../image/style/t2.png" /><br>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	return $xml;
}
function submitzthhgd($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$c = $posti['radio'] = 'bha'?'true':'false';//判断操作者是否选择“已闭环”
		$db->ExeSql("insert into cu_pg(gd_cn,gd_time,pg_name,pg_rechtime,pg_begintime,pg_yjbx,pg_sjbx,traceman,oman,bz,bh) values ('$cn','$time','$posti[hjlb]','$posti[rechtime]',now(),'$posti[yjbx]','$posti[sjbx]','$posti[traceman]','$posti[oman]','$posti[hjbz]','$c')");
		$db->ExeSql("update cu_glxq set hhj=true where contactnumber='$cn'");
		$db->FreeResult();
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的环节已提交，请重新查询！</div>';
	}else{
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
	}
	return $xml;
}
function submitzthhud($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_pg` WHERE  gd_cn='$cn' AND pg_begintime='$time'");
	if ($db->FetchRow()){
		$c = $posti['radio'] = 'bha'?'true':'false';//判断操作者是否选择“已闭环”
		$db->ExeSql("update cu_pg set pg_rechtime='$posti[rechtime]',pg_yjbx='$posti[yjbx]',pg_sjbx='$posti[sjbx]',traceman='$posti[traceman]',oman='$posti[oman]',bz='$posti[hjbz]',bh='$c' where gd_cn='$cn' and pg_begintime='$time'");
		$db->ExeSql("update cu_glxq set hhj=true where contactnumber='$cn'");
		$db->FreeResult();
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的环节已提交，请重新查询！</div>';
	}else{
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
	}
	return $xml;
}
function showzthh($cn,$time){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if($gdclroot==true){
		$db->ExeQuerySql("select * from cu_glxq where contactnumber='$cn' and time='$time'");
		if ($db->FetchRow()) {
			if ($db->Get('isend')=="归档") {
				$xml = file_get_contents("./tpl/tpl.zthh.html");
				$table_change=array(
					'{cn}' => $cn,
					'{time}' => $time,
					'{now_em}' => $now_em,
					'{now_om}' => $db->Get('customername')
				);
				$xml=strtr($xml,$table_change);
			}else {
				$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前的工单没有归档，请确认归档之后再处理退换货！</div>';
			}
		}
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	return $xml;
}
function updatehj($cn,$time){
	global $now_user;
	global $ROOT;
	global $MEMBER;
	global $db;
	$now_em = $MEMBER->getnickname($_SESSION['uid']);
	$gdclroot=$ROOT->cigdcl($_SESSION['uid']);
	if($gdclroot==true){
		$db->ExeQuerySql("select * from cu_pg where gd_cn='$cn' and pg_begintime='$time'");
		if ($db->FetchRow()) {
				$xml = file_get_contents("./tpl/tpl.edithj.html");
				$table_change=array(
						'{cn}' => $cn,
						'{time}' => $time,
						'{now_em}' => $now_em,
						'{now_om}' => $db->Get('oman'),
						'{pg_name}' => $db->Get('pg_name'),
						'{hjbz}' =>$db->Get('bz')
				);
				$xml=strtr($xml,$table_change);
		}
	}else{
		$xml='<div id="gdc"><div class="vbr">工單處理</div>对不起，你目前无法处理工单，可能你没有处理工单的权限，请与管理员联系！</div>';
	}
	return $xml;
}
function sumbitgdpg($cn,$time,$posti){
	global $db;
	$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，数据库可能异常，请重新查询！</div>';
	$db->ExeQuerySql("SELECT * FROM `cu_glxq` WHERE  contactnumber='$cn' AND time='$time'");
	if ($db->FetchRow()){
		$db->ExeSql("update `cu_glxq` set ctrlman='$posti[chuliren]',isend='$posti[gdlb]',endtime=now(),archivecontent='$posti[archivecontent]',gdqc='$posti[gdqc]' where contactnumber='$cn' AND time='$time'");
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>完成，你目前的工单已处理，请重新查询！</div>';
	}else{
		$xml = '<div id="gdc"><div class="vbr">工單處理</div>目前没有完成，可能此数据已删除或异常，请重新查询！</div>';
	}
	return $xml;
}
echo $returnajaxcontent;